var FieldMove = function()
{
	this.data = {
		wheel_step: 40,
		first_click_top: NaN,
		last_field_position_top: 0
	};
	
    this.elements = {
        overflow_panel: null,
        field: null
    };

    this.init = function()
    {
        this.elements.overflow_panel = $('#overflow_panel'); 
        this.elements.field = $('#livehabr_box'); 
        
        this.elements.overflow_panel
            .mousedown(function(e){
                FieldMove.elements.overflow_panel.attr('class', 'cur_hand_close');
                FieldMove.startFieldMove(e);
            })
            .mouseup(function(e){
                FieldMove.elements.overflow_panel.attr('class', 'cur_hand_open');
                FieldMove.stopFieldMove(e);
            })
            .bind('mousewheel', function(e, delta){
            	var pos_top = FieldMove.data.last_field_position_top;
                if (delta > 0) {
                	pos_top += FieldMove.data.wheel_step;
                } else {
                	pos_top -= FieldMove.data.wheel_step;
                }
                if (pos_top > 0) {
                	pos_top = 0;
                }
              	FieldMove.setFieldTop(pos_top);
              	FieldMove.data.last_field_position_top = pos_top;
            });
    };
    
    this.startFieldMove = function(e)
    {
    	this.data.last_field_position_top = this.elements.field.position().top;
    	this.data.first_click_top = e.clientY;
        this.elements.overflow_panel.bind('mousemove', function(e){
            FieldMove.setFieldTop(
                FieldMove.data.last_field_position_top
              + e.clientY
              - FieldMove.data.first_click_top
            );
        });
    };

    this.stopFieldMove = function(e)
    {
        this.elements.overflow_panel.unbind('mousemove');
        this.setFieldTop(
            FieldMove.data.last_field_position_top
          + e.clientY
          - FieldMove.data.first_click_top
        );
    	this.data.last_field_position_top = this.elements.field.position().top;
    };

    this.setFieldTop = function(position_top) {
    	if (position_top <= 0) {
	    	this.elements.field.css({top: position_top});
    	} else {
    		FieldMove.data.first_click_top += position_top; 
    	}
    };
    
    this.onResize = function(width, height)
    {
        this.elements.overflow_panel.css({
            width: width,
            height: height
        });
    };
};
FieldMove = new FieldMove;